package ru.aslanov.schedule.client;

import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.types.DateDisplayFormat;
import com.smartgwt.client.types.TimeFormatter;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.calendar.Calendar;
import com.smartgwt.client.widgets.calendar.CalendarEvent;
import com.smartgwt.client.widgets.calendar.events.CalendarEventClick;
import com.smartgwt.client.widgets.calendar.events.EventClickHandler;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import ru.aslanov.schedule.client.ds.CalendarEventsDS;
import ru.aslanov.schedule.client.ds.LocationsDS;
import ru.aslanov.schedule.client.ds.TeachersDS;

import static ru.aslanov.schedule.client.i18n.I18nUtil.cnt;

/**
 * @author Sergey Aslanov
 *         Created: 1/27/11 5:26 PM
 */
public class CalendarViewPanel extends VLayout implements Invalidatable {

    private Calendar calendar;
    private SelectItem teachersSelect;
    private SelectItem locationsSelect;

    public CalendarViewPanel(ScheduleUser scheduleUser, final SchedulePanel schedulePanel) {
        super(10);
        HLayout buttons = new HLayout(5);
        buttons.setHeight(15);
        addMember(buttons);

        this.locationsSelect = new SelectItem();
        this.locationsSelect.setAllowEmptyValue(true);
        this.locationsSelect.setTitle(cnt().locations());
        this.locationsSelect.setOptionDataSource(LocationsDS.getInstance(scheduleUser));
        this.locationsSelect.setValueField("encodedKey");
        this.locationsSelect.setDisplayField("name");

        this.teachersSelect = new SelectItem();
        this.teachersSelect.setAllowEmptyValue(true);
        this.teachersSelect.setTitle(cnt().teachers());
        this.teachersSelect.setOptionDataSource(TeachersDS.getInstance(scheduleUser));
        this.teachersSelect.setValueField("encodedKey");
        this.teachersSelect.setDisplayField("name");

        DynamicForm form = new DynamicForm();
        form.setFields(this.locationsSelect, this.teachersSelect);
        form.setNumCols(4);

        Button refreshButton = new Button(cnt().refreshButton());
        refreshButton.setAutoFit(true);
        refreshButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                calendar.invalidateCache();
                refreshData();
            }
        });
        buttons.addMember(form);
        buttons.addMember(refreshButton);

        calendar = new Calendar();
        //use 24 hr based times
        calendar.setTimeFormatter(TimeFormatter.TOSHORT24HOURTIME);
        //use european based DD/MM/YYYY date formats
        calendar.setDateFormatter(DateDisplayFormat.TOEUROPEANSHORTDATE);

        calendar.setFirstDayOfWeek(1); // MONDAY
        //calendar.setShowWorkday(true);
        calendar.setWorkdays(new int[]{0,1,2,3,4,5,6});
        calendar.setWorkdayStart("03:00pm");
        calendar.setWorkdayEnd("11:30pm");
        calendar.setScrollToWorkday(true);

        calendar.setShowDateChooser(false);
        calendar.setShowAddEventButton(false);
        calendar.setShowDatePickerButton(false);
        calendar.setShowMonthView(false);
        calendar.setCanDeleteEvents(false);
        calendar.setCanCreateEvents(false);
        calendar.setCanEditEvents(false);

        calendar.setDataSource(CalendarEventsDS.getInstance(scheduleUser));
        calendar.setAutoFetchData(false);

        calendar.addEventClickHandler(new EventClickHandler() {
            @Override
            public void onEventClick(CalendarEventClick calendarEventClick) {
                final String groupKey = calendarEventClick.getEvent().getAttribute("groupKey");
                schedulePanel.editGroup(groupKey);
            }
        });

        addMember(calendar);

        this.locationsSelect.addChangedHandler(new ChangedHandler() {
            @Override
            public void onChanged(ChangedEvent event) {
                teachersSelect.setValue((String) null);
                refreshData();
            }
        });
        this.teachersSelect.addChangedHandler(new ChangedHandler() {
            @Override
            public void onChanged(ChangedEvent event) {
                locationsSelect.setValue((String) null);
                refreshData();
            }
        });
    }

    private void refreshData() {
        String locationKey = locationsSelect.getSelectedRecord() != null ? locationsSelect.getSelectedRecord().getAttribute("encodedKey") : null;
        if (locationKey != null) {
            calendar.fetchData(new Criteria("locationKey", locationKey));
        } else {
            String teacherKey = teachersSelect.getSelectedRecord() != null ? teachersSelect.getSelectedRecord().getAttribute("encodedKey") : null;
            if (teacherKey != null) {
                calendar.fetchData(new Criteria("teacherKey", teacherKey));
            } else {
                calendar.setData(new CalendarEvent[]{});
            }
        }
    }

    @Override
    public void invalidateData() {
        calendar.invalidateCache();
        refreshData();
    }
}
